home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SPX30.ZIP / DEMO09.ZIP / DEMO09.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-06-13  |  5.0 KB  |  188 lines

  1. Program Demo9;
  2.  
  3. { SPX library - Geomorph HexMap demo Copyright 1993 Scott D. Ramsay  }
  4.  
  5. Uses crt,spx_vga,spx_vsp,spx_fnc,spx_sfn,spx_geo,spx_key;
  6.  
  7. const
  8.   path = '';      { default path for files }
  9.   gmx  = 50;      { geomorph width }
  10.   gmy  = 50;      { geomorph height }
  11.   sp   = 6;       { scroll speed }
  12.  
  13. type
  14.   ThexPos     = record
  15.                   hexcol,hexrow : byte;
  16.                 end;
  17.   PMyHexMorph = ^TMyHexMorph;
  18.   TMyHexMorph = object(THexMorph)
  19.                   function geomap(x,y:integer):integer;virtual;
  20.                   procedure placegeo(x,y,geonum,cx,cy:integer);virtual;
  21.                   procedure nogogeo(x,y,cx,cy:integer);virtual;
  22.                 end;
  23.  
  24. var
  25.   hexes : array[0..7] of pointer;               { hold loaded sprites }
  26.   vx,vy,                                        { object's pixel position }
  27.   hcx,hcy : integer;                            { current drawn hex map pos }
  28.   map   : array[0..gmy-1,0..gmx-1] of byte;     { hex map - geomorph }
  29.   mm    : PMyHexMorph;
  30.   h1,                                           { object's hex coordinates }
  31.   h2    : THexPos;                              { random target coord }
  32.  
  33. { Create a random geomorph }
  34. procedure createmap;
  35. var
  36.   d,e : integer;
  37. begin
  38.   for d := 0 to gmy-1 do
  39.     for e := 0 to gmx-1 do
  40.       map[d,e] := random(5)+2; { use only sprites 2..6 }
  41. end;
  42.  
  43. { draw the screen }
  44. procedure drawscreen;
  45. begin
  46.   rectangle(9,9,161,161,4);
  47.   putletter(180,20,15,'Hex Map test');
  48.   putletter(180,60,9,'USE ARROW KEYS TO SCROLL MAP');
  49.   putletter(180,67,9,'PRESS ESC TO QUIT');
  50.   putletter(10,165,4,'Object position:');
  51.   putletter(10,172,4,'Target position:');
  52.   putletter(SFNx,172,12,st(h2.hexcol)+','+st(h2.hexrow));
  53. end;
  54.  
  55.  
  56. { Set variables and screen }
  57. procedure setup;
  58. begin
  59.   openmode(1);                      { open vga 320x200x256 mode }
  60.   randomize;                        { set random seed }
  61.   loadvsp(path+'hex2.vsp',hexes);   { load sprites }
  62.   createmap;                        { create map }
  63.   mm := new(PMyHexMorph,init(gmx,gmy,13,12,14,14,0,0)); { init HexMap }
  64.  { The Y position of the odd columns will be offset by 6.  The }
  65.  { first column is even (0) }
  66.   mm^.oddy := 6;
  67.   vx := 0; vy := 0;      { Set objects starting position }
  68.   h2.hexcol := random(gmx);     { Set random object position }
  69.   h2.hexrow := random(gmy);
  70.   drawscreen;            { Draw screen }
  71. end;
  72.  
  73.  
  74. { Get keyboard input }
  75. procedure getinput;
  76. var
  77.   ox,oy : integer;
  78. begin
  79.   ox := h1.hexcol; oy := h2.hexrow; { save old object position }
  80.   if (np[7,2] or np[8,2] or np[9,2])
  81.     then dec(vy,sp) { move up }
  82.     else
  83.       if (np[1,2] or np[2,2] or np[3,2])
  84.         then inc(vy,sp); { move down }
  85.   if (np[7,2] or np[4,2] or np[1,2])
  86.     then dec(vx,sp) { move left }
  87.     else
  88.       if (np[9,2] or np[6,2] or np[3,2])
  89.         then inc(vx,sp); { move right }
  90.  { make sure VX,VY is always in the legal ranges }
  91.   ifix(vx,0,gmx*mm^.gsx-1);
  92.   ifix(vy,0,gmy*mm^.gsy-1);
  93.  { Calcuate the actual tile location }
  94.   h1.hexcol := vx div mm^.gsx; h1.hexrow := vy div mm^.gsy;
  95.  { print stats on screen }
  96.   if (h1.hexcol<>ox) or (h1.hexrow<>oy)
  97.     then
  98.       begin
  99.         bar(69,165,100,170,0);
  100.         putletter(69,165,12,st(h1.hexcol)+','+st(h1.hexrow));
  101.       end;
  102. end;
  103.  
  104.  
  105. { program loop }
  106. procedure ani;
  107. begin
  108.   repeat
  109.     getinput;                 { get keyboard input }
  110.     mm^.drawmap(vx,vy);       { draw the map }
  111.   until key[KEY_ESC];         { Press ESC to quit }
  112. end;
  113.  
  114.  
  115. { Set the screen clipping region on or off }
  116. procedure hexSetClip(on:boolean);
  117. begin
  118.   if on
  119.     then
  120.       begin
  121.         WinMinX := 10; WinMinY := 10;
  122.         WinMaxX := 160; WinMaxY := 160;
  123.       end
  124.     else
  125.       begin
  126.         WinMinX := 0; WinMinY := 0;
  127.         WinMaxX := 320; WinMaxY := 200;
  128.       end;
  129. end;
  130.  
  131. (**) { TMyHexMorph }
  132.  
  133. function TMyHexMorph.geomap(x,y:integer):integer;
  134. begin
  135.   geomap := map[y,x];
  136.   hcx := x; hcy := y;
  137. end;
  138.  
  139.  
  140. procedure TMyHexMorph.nogogeo(x,y,cx,cy:integer);
  141. begin
  142.   hexSetClip(true);
  143.   ftput_clip(x,y,hexes[0]^,false);
  144.   hexSetClip(false);
  145. end;
  146.  
  147.  
  148. procedure TMyHexMorph.placegeo(x,y,geonum,cx,cy:integer);
  149. begin
  150.   if geonum>0
  151.     then
  152.       begin
  153.       { display the tiles, display the object if its on this tile }
  154.         hexSetClip(true);
  155.         if (h1.hexcol=hcx) and (h1.hexrow=hcy)
  156.           then ftput_clip(x,y,hexes[6]^,false)
  157.           else
  158.         if (h2.hexcol=hcx) and (h2.hexrow=hcy)
  159.           then ftput_clip(x,y,hexes[7]^,false)
  160.           else ftput_clip(x,y,hexes[geonum-1]^,false);
  161.         hexSetClip(false);
  162.       end;
  163. end;
  164.  
  165.  
  166. procedure showit;
  167. begin
  168.   clrscr;
  169.   writeln('SPX library - Geomorph demo 2 - HexMap ');
  170.   writeln('Copyright 1993 Scott D. Ramsay');
  171.   writeln;
  172.   writeln('Keys:');
  173.   writeln(' ESC          - quit demo');
  174.   writeln(' Arrow keys   - move object');
  175.   writeln;
  176.   write('Press SPACE to continue.');
  177.   clearbuffer;
  178.   repeat until key[KEY_SPACE];
  179. end;
  180.  
  181.  
  182. begin
  183.   showit;
  184.   setup;
  185.   ani;
  186.   closemode;
  187. end.
  188.